﻿using System;
using System.Configuration;
using System.Web.Configuration;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class CustomErrorsSectionManager : ConfigurationManagerBase
	{
		private CustomErrorsSection section;

		public CustomErrorsSectionManager()
		{
			this.section = (CustomErrorsSection)base.GetConfigurationSection("system.web/customErrors");
		}

		public void Modify(CustomErrorsMode mode, string defaultRedirect)
		{
			this.section.Mode = mode;
			this.section.DefaultRedirect = defaultRedirect;
			this.Save();
		}
		public CustomErrorsSection GetCustomErrors()
		{
			return this.section;
		}

		public void AddError(int statusCode, string redirect)
		{
			if (string.IsNullOrEmpty(redirect))
			{
				throw new ArgumentNullException("redirect");
			}

			CustomError _newError = new CustomError(statusCode, redirect);
			this.section.Errors.Add(_newError);
			this.Save();
		}
		public void ModifyError(string statusCode, string redirect)
		{
			if (string.IsNullOrEmpty(statusCode))
			{
				throw new ArgumentNullException("statusCode");
			}
			if (string.IsNullOrEmpty(redirect))
			{
				throw new ArgumentNullException("redirect");
			}
			this.section.Errors[statusCode].Redirect = redirect;
			this.Save();
		}
		public void RemoveError(string statusCode)
		{
			if (string.IsNullOrEmpty(statusCode))
			{
				throw new ArgumentNullException("statusCode");
			}
			this.section.Errors.Remove(statusCode);
			this.Save();
		}
		public void ClearErrors()
		{
			this.section.Errors.Clear();
			this.Save();
		}
		public CustomError GetErrors(string statusCode)
		{
			return this.section.Errors[statusCode];
		}
		public CustomErrorCollection GetErrors()
		{
			return this.section.Errors;
		}
	}
}